home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / AppleScript for Acrobat plug-in / MAC / AcrobatAppleScript.c next >
Encoding:
C/C++ Source or Header  |  2000-06-24  |  3.7 KB  |  162 lines

  1. #include "AcrobatAppleScript.h"
  2.  
  3. #include "ASCalls.h"
  4. #include "AVCalls.h"
  5. #include "CorCalls.h"
  6. #include "PDCalls.h"
  7. #include "CosCalls.h"
  8.  
  9. static pascal Boolean MyEventFilter( DialogPtr theDialog, EventRecord theEvent, DialogItemIndex itemHit )
  10. {
  11.     Boolean    handledIt;
  12.     
  13.     handledIt = false;
  14.     
  15.     if( theEvent.what == keyDown )
  16.     {
  17.         itemHit = 3;
  18.     }
  19.     
  20.     return( handledIt );
  21. }
  22.  
  23. Boolean AddModifyScriptDialog( char *theScript )
  24. {
  25.  
  26.     Boolean                done;
  27.     OSErr                err;
  28.     DialogPtr            theEditorDialog;
  29.     DialogItemIndex        index;
  30.     DialogItemType        theType;
  31.     Handle                theHandle;
  32.     Rect                theBox;
  33.     ModalFilterUPP        theFilterProc;
  34.     
  35.     theFilterProc = NewModalFilterUPP( MyEventFilter );
  36.     
  37.     /* If I was paying attention to the API in Acrobat, this would be SafeGetNewDialog */        
  38.     theEditorDialog = GetNewDialog( 1024, nil, (WindowPtr)(-1));
  39.     
  40.     if( theEditorDialog != nil )
  41.     {
  42.     
  43.         GetDialogItem( theEditorDialog, 3, &theType, &theHandle, &theBox );
  44.         if( theHandle != nil )
  45.         {
  46.             setdialogitemtext( theHandle, theScript );
  47.         }
  48.  
  49.         err = SetDialogDefaultItem( theEditorDialog, 1 );
  50.         
  51.         err = SetDialogCancelItem( theEditorDialog, 2 );
  52.         
  53.         done = false;
  54.         
  55.         do {
  56.         
  57.             ModalDialog( theFilterProc, &index );
  58.         
  59.             if( index == 1 ) 
  60.                 done = true;
  61.         
  62.             if( index == 2 )
  63.                 done = true;
  64.  
  65.         } while ( done == false );
  66.         
  67.         if( index == 1 )
  68.         {
  69.             getdialogitemtext( theHandle, theScript );
  70.         }
  71.         
  72.         DisposeDialog( theEditorDialog );
  73.     }
  74.     
  75.     DisposeModalFilterUPP( theFilterProc );
  76.     
  77.     if( index == 1 )
  78.         return true;
  79.         
  80.     return false;
  81. }
  82.  
  83. Boolean IsKeyDown( short whichKeyCode )
  84. {
  85.     unsigned char km[16];
  86.     GetKeys((unsigned long *) km );
  87.     return(( km[whichKeyCode>>3]>>(whichKeyCode&7)) & 1 );
  88.  
  89. }
  90. void GetScriptFromAnnot( PDAnnot annot, char *theScript )
  91. {
  92.     char    *scriptPtr;
  93.     CosObj    cAnnotObj, scriptObj, theScriptObj, nullObj;
  94.     CosType    scriptType;
  95.     ASInt32    nBytes;
  96.     ASAtom    scriptArray;
  97.  
  98.     nullObj = CosNewNull( );
  99.  
  100.     /* get the annotation CosObj */
  101.     cAnnotObj = PDAnnotGetCosObj(annot);
  102.     
  103.     if( CosDictKnown( cAnnotObj, ASAtomFromString( "AppleScript" )) == true )
  104.     {
  105.         scriptObj = CosDictGet( cAnnotObj, ASAtomFromString( "AppleScript" ));
  106.         
  107.         /* make sure that what comes out of the annot cos object isn't null */
  108.         if( CosObjEqual( scriptObj, nullObj ) == false )
  109.         {
  110.             /* get the actual script out of the script CosObj */
  111.             theScriptObj = CosDictGet( scriptObj, ASAtomFromString( "TheScript" ));
  112.             
  113.             if( CosObjEqual( theScriptObj, nullObj ) == false )
  114.             {
  115.                 scriptType = CosObjGetType( theScriptObj );
  116.             
  117.                 nBytes = 0;
  118.                 scriptPtr = CosStringValue( theScriptObj, &nBytes );
  119.                 
  120.                 strncpy( theScript, scriptPtr, ( nBytes < kScriptBufferSize ? nBytes : kScriptBufferSize ));
  121.             }
  122.         }
  123.     }
  124. }
  125.  
  126. void DoScript( char *theScript )
  127. {
  128.     ComponentInstance    scriptingComponent;
  129.     AEDesc                componentName, scriptText, resultText;
  130.     OSAID                scriptID, resultID;
  131.     OSAError            myOSAError, ignoreErr;
  132.     OSErr                err;
  133.     
  134.     scriptingComponent = OpenDefaultComponent( kOSAComponentType, kOSAGenericScriptingComponentSubtype );
  135.     myOSAError = OSAScriptingComponentName( scriptingComponent, &componentName );
  136.     if( myOSAError == noErr )
  137.     {
  138.         err = AECreateDesc( typeChar, theScript, strlen( theScript ), &scriptText );
  139.         if( err == noErr )
  140.         {
  141.             scriptID = kOSANullScript;
  142.             myOSAError = OSACompile( scriptingComponent, &scriptText, kOSAModeNull, &scriptID );
  143.         
  144.             if( myOSAError == noErr )
  145.             {
  146.                 myOSAError = OSAExecute( scriptingComponent, scriptID, kOSANullScript, kOSAModeNull, &resultID );
  147.                 ignoreErr = OSADispose( scriptingComponent, scriptID );
  148.                 
  149.                 if( myOSAError != noErr )
  150.                 {
  151.                     AVAlert( ALERT_STOP, "Couldn't Execute Script", "OK", nil, nil, true );
  152.                 }
  153.             } else {
  154.                 AVAlert( ALERT_STOP, "Couldn't Compile Script", "OK", nil, nil, true );
  155.             }
  156.             
  157.             ignoreErr = AEDisposeDesc( &scriptText );
  158.         }
  159.     }
  160. }
  161.  
  162.